package vistas.ventanas;

import javax.swing.*;

import daos.UsuariosDAO;
import entidades.Usuario;
import entidades.enums.Privilegio;

import java.awt.*;
import java.awt.event.*;

public class LoginDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel label_usuario;
	private JLabel label_clave;
	private JTextField usuario;
	private JPasswordField clave;
	private JPanel contenedor, labels, text_fields, botones, mid, bottom;
	private JButton salir, aceptar;
	private Boolean ok = false;
	private UsuariosDAO usuariosDAO;
	private Usuario user = null;

	public LoginDialog(Frame parent) {
		super(parent, "Login", true);
		setTitle("Login");
		setPreferredSize(new Dimension(300, 120));
		setResizable(false);
		setLocationRelativeTo(null);

		aceptar = new JButton("Aceptar");
		salir = new JButton("Salir");
		label_usuario = new JLabel("Usuario:");
		label_clave = new JLabel("Password:");
		usuario = new JTextField(12);
		clave = new JPasswordField(12);
		contenedor = new JPanel();
		labels = new JPanel();
		text_fields = new JPanel();
		botones = new JPanel();
		mid = new JPanel();
		bottom = new JPanel();
		
		usuariosDAO = new UsuariosDAO();

		contenedor.setLayout(new BoxLayout(contenedor, BoxLayout.PAGE_AXIS));
		contenedor.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10));
		mid.setLayout(new BoxLayout(mid, BoxLayout.X_AXIS));
		labels.setLayout(new BoxLayout(labels, BoxLayout.Y_AXIS));
		labels.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
		text_fields.setLayout(new BoxLayout(text_fields, BoxLayout.Y_AXIS));
		botones.setLayout(new BoxLayout(botones, BoxLayout.X_AXIS));
		bottom.setLayout(new BorderLayout());

		botones.add(aceptar);
		botones.add(Box.createRigidArea(new Dimension(5, 0)));
		botones.add(salir);

		labels.add(label_usuario);
		labels.add(Box.createRigidArea(new Dimension(0, 20)));
		labels.add(label_clave);

		text_fields.add(usuario);
		text_fields.add(Box.createRigidArea(new Dimension(0, 5)));
		text_fields.add(clave);

		mid.add(labels);
		mid.add(text_fields);

		bottom.add(botones, BorderLayout.EAST);

		contenedor.add(mid);
		contenedor.add(bottom);

		pack();

		add(contenedor);
		setListeners();

	}
	
	public Boolean getOk(){
		return ok;
	}
	
	public Usuario getUser(){
		return user;
		
	}
	
	void setListeners() {

		aceptar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					String aux = new String(clave.getPassword());
					if(usuario.getText().equals("root") && aux.equals("root")){
						ok = true;
						dispose();
						user = new Usuario();
						user.setPrivilegio(Privilegio.administrador);
						return;
					}
					user = usuariosDAO.consultarUsuario(usuario.getText());
					if( user.getContrasena().equals(aux)){
						ok = true;
						dispose();
						return;
					}
					else{
						JOptionPane.showMessageDialog(null, "El usuario o clave son inválidos");
						clave.setText("");
						usuario.setText("");
						return;
					} 
						
				} catch (Exception e2) {
					JOptionPane.showMessageDialog(null, "El usuario o clave son inválidos");
					clave.setText("");
					usuario.setText("");
					return;
				}
				

			}
		});
		
		salir.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
				
			}
		});

	}

}
